import Vue from 'vue'
import VueRouter from 'vue-router'
import Layout from '@/layout'
Vue.use(VueRouter)
/**
 * 为什么设计路由时要使用子路由
 *  假设轮播图管理，目前只有一个轮播图列表，如果以后再加其他选项，可以直接在子路由中配置
 *   可以直接在左侧菜单栏中展示
 *
 *渲染左侧菜单栏时，通过判断当前的路由有没有children选项，或者即使有children的选项，判断其长度
 如果没有children选项或者长度为1，表示是一级路由
 如果长度大于1，可以显示为二级路由
 如果子路由中也有children选项的选项，也是同样的判断
 ------- 递归调用vue的组件
 */

// 所有的用户均可以访问的路由，不需要权限校验
export const constantRoutes = [
  {
    path: '/login',
    name: 'login',
    component: () => import('@/views/login/index.vue'),
    hidden: true // 不显示在左侧菜单栏
  },
  {
    path: '/',
    component: Layout,
    redirect: '/home',
    children: [
      {
        path: 'home',
        name: 'home',
        component: () => import('@/views/home/index.vue'),
        meta: { title: '首页', icon: 'home' }
      }
    ]
  },
  {
    path: '/banner',
    component: Layout,
    redirect: '/banner/list',
    meta: { title: '轮播图管理', icon: 'car' },
    children: [
      {
        path: 'list',
        name: 'bannerlist',
        component: () => import('@/views/banner/index.vue'),
        meta: { title: '轮播图列表', icon: 'appstore', len: 2 }
      },
      {
        path: 'add',
        name: 'banneradd',
        component: () => import('@/views/banner/add.vue'),
        meta: { title: '添加轮播图', icon: 'appstore', len: 2 }
      }
    ]
  },
  {
    path: '/pro',
    component: Layout,
    redirect: '/pro/list',
    meta: { title: '产品管理', icon: 'car' },
    children: [
      {
        path: 'list',
        name: 'prolist',
        component: () => import('@/views/pro/index.vue'),
        meta: { title: '产品列表', icon: 'code', len: 2 }
      },
      {
        path: 'recommend',
        name: 'recommendlist',
        component: () => import('@/views/pro/recommend.vue'),
        meta: { title: '推荐列表', icon: 'container', len: 2 }
      },
      {
        path: 'seckill',
        name: 'seckilllist',
        component: () => import('@/views/pro/seckill.vue'),
        meta: { title: '秒杀列表', icon: 'book', len: 2 }
      }
    ]
  },
  {
    path: '/user',
    component: Layout,
    redirect: '/user/list',
    children: [
      {
        path: 'list',
        name: 'userlist',
        component: () => import('@/views/user/index.vue'),
        meta: { title: '用户管理', icon: 'bell' }
      }
    ]
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes: constantRoutes
})

export default router
